import Vue from "vue";
import Vuex from "vuex";
import persistence from 'vuex-persistedstate'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    Detail: {},
    shopping: [],
  },
  mutations: {
    // 商品详情
    Details(state, obj) {
      state.Detail = obj
    },
    // 购物车
    shops(state, obj) {
      if (state.shopping.findIndex(item => { return item.name == obj.name }) != -1) {
        state.shopping[state.shopping.findIndex(item => { return item.name == obj.name })].num++;
      } else {
        state.shopping.push(obj);
      }
      console.log(state.shopping);
    },
    removeShopping(state) {
      state.shopping = [];
    }
  },
  actions: {},
  modules: {},
  getters: {},
  plugins: [persistence()],//引入插件

});
